import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DialogUtil {
  static void bottomListSelectDialog(BuildContext context, int count,
      {String title = '请选择',
      double itemHeight = 0.0,
      String Function(int index)? buildItemText,
      bool Function(int index)? hasSelectedItem,
      void Function(int index)? onSelectItem}) {
    showModalBottomSheet(
        context: context,
        isScrollControlled: true,
        backgroundColor: Colors.transparent,
        builder: (ctx) {
          return _buildSelectListDialog(context, title, count,
              itemHeight: itemHeight,
              buildItemText: buildItemText,
              hasSelectedItem: hasSelectedItem,
              onSelectItem: onSelectItem);
        });
  }

  static Widget _buildSelectListDialog(
      BuildContext context, String title, int count,
      {double itemHeight = 0.0,
      String Function(int index)? buildItemText,
      bool Function(int index)? hasSelectedItem,
      void Function(int index)? onSelectItem}) {
    if (itemHeight == 0.0) itemHeight = 40.w; //默认高度
    double dialogHeight = count * itemHeight.w + 45.w + count;
    if (dialogHeight > 2 * ScreenUtil.defaultSize.height / 3)
      dialogHeight = 2 * ScreenUtil.defaultSize.height / 3; //最大高度
    if (dialogHeight < 180.w) dialogHeight = 180.w; //最小高度

    dialogHeight += 15.w;

    return Container(
      decoration: new BoxDecoration(
          borderRadius: new BorderRadius.only(
              topLeft: Radius.circular(12.w), topRight: Radius.circular(12.w)),
          color: Colors.white),
      height: dialogHeight,
      child: Column(
        children: [
          Container(
            height: 45.w,
            padding: EdgeInsets.only(left: 12.w, right: 12.w),
            child: Row(
              children: [
                Text(
                  title,
                  style: TextStyle(color: Colors.black, fontSize: 16.sp),
                ),
                Spacer(),
                InkWell(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Image.asset(
                    'assets/images/icon_item_delete.png',
                    width: 20.w,
                    height: 20.w,
                  ),
                )
              ],
            ),
          ),
          Divider(
            height: 1,
          ),
          Expanded(
            child: ListView.separated(
              itemCount: count,
              separatorBuilder: (context, index) {
                return Divider(
                  height: 1,
                );
              },
              itemBuilder: (context, index) {
                return Container(
                  height: itemHeight,
                  padding: EdgeInsets.only(left: 10.w, right: 10.w),
                  child: InkWell(
                    onTap: () {
                      if (onSelectItem != null) {
                        onSelectItem(index);
                        Navigator.pop(context);
                      }
                    },
                    child: Row(
                      children: [
                        Expanded(
                          child: Text(
                            buildItemText != null
                                ? buildItemText(index)
                                : '$index',
                            maxLines: 2,
                            style: TextStyle(
                                color: hasSelectedItem != null &&
                                        hasSelectedItem(index)
                                    ? Color(0xff009669)
                                    : Color(0xff333333),
                                fontSize: 14.sp),
                          ),
                        ),
                        SizedBox(
                          width: 10.w,
                        ),
                        Offstage(
                          offstage: hasSelectedItem != null
                              ? !hasSelectedItem(index)
                              : true,
                          child: Image.asset(
                            'assets/images/ic_selected_mark.png',
                            width: 16.w,
                            height: 16.w,
                          ),
                        )
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
